package com.mdx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdx.bean.ClassBean;
import com.mdx.bean.RegionBean;
import com.mdx.dao.ClassDao;
import com.mdx.service.IClassService;
import com.mdx.service.IRegionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class ClassServiceImpl extends ServiceImpl<ClassDao, ClassBean> implements IClassService {

    @Resource
    private IRegionService regionService;


    @Override
    public void addClass(ClassBean classBean) {
        this.baseMapper.addClass(classBean);
        RegionBean regionBean = regionService.getById(classBean.getRegionBean().getRegionId());
        regionBean.setClassName(classBean.getClassName());
        regionService.updateById(regionBean);
    }

    @Override
    public void updateHeadAndTel(int classId, String head, String headTel) {
        QueryWrapper wrapper = new QueryWrapper();
        ClassBean classBean = this.getById(classId);
        classBean.setHead(head);
        classBean.setHeadTel(headTel);
        this.update(classBean,wrapper);
    }

    @Override
    public IPage<ClassBean> showClassByItem(int pageNO, int pageSize, String className, String regionName) {
        IPage<ClassBean> page = new Page<>(pageNO, pageSize);
        return this.baseMapper.showClassByItem(page,className,regionName);
    }
}
